package com.code.train.resp;

import com.code.train.exception.BusinessExceptionEnum;
import com.code.train.respCode.RespCode;
import lombok.Builder;
import lombok.Data;

/**
 * 通用响应体封装类
 */
@Data
@Builder
public class CommonResp<T> {

    /**
     * 是否成功（业务成功）
     */
    @Builder.Default
    private boolean success = true;

    /**
     * 返回码（可选）
     */
    @Builder.Default
    private String code = RespCode.SUCCESS.getCode();

    /**
     * 返回消息
     */
    @Builder.Default
    private String message = RespCode.SUCCESS.getMessage();

    /**
     * 返回数据体
     */
    private T content;

    // ----------------- 静态构建方法 -----------------

    public static <T> CommonResp<T> success() {
        return CommonResp.<T>builder().build();
    }

    public static <T> CommonResp<T> success(T content) {
        return CommonResp.<T>builder()
                .content(content)
                .build();
    }

    public static <T> CommonResp<T> success(String message, T content) {
        return CommonResp.<T>builder()
                .message(message)
                .content(content)
                .build();
    }

    public static <T> CommonResp<T> fail(String message) {
        return CommonResp.<T>builder()
                .success(false)
                .code(RespCode.FAIL.getCode())
                .message(message)
                .build();
    }

    public static <T> CommonResp<T> fail(String code, String message) {
        return CommonResp.<T>builder()
                .success(false)
                .code(code)
                .message(message)
                .build();
    }

    public static <T> CommonResp<T> fail(RespCode codeEnum) {
        return CommonResp.<T>builder()
                .success(false)
                .code(codeEnum.getCode())
                .message(codeEnum.getMessage())
                .build();
    }

    public static <T> CommonResp<T> fail(BusinessExceptionEnum businessExceptionEnum) {
        return CommonResp.<T>builder()
                .success(false)
                .code(businessExceptionEnum.getCode())
                .message(businessExceptionEnum.getDesc())
                .build();
    }

    // 可以根据需要继续拓展更多构建方式
}

